<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单id" prop="orderid">
      <el-input v-model="dataForm.orderid" placeholder="订单id"></el-input>
    </el-form-item>
    <el-form-item label="直播名称" prop="livename">
      <el-input v-model="dataForm.livename" placeholder="直播名称"></el-input>
    </el-form-item>
    <el-form-item label="直播id" prop="liveid">
      <el-input v-model="dataForm.liveid" placeholder="直播id"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="goodid">
      <el-input v-model="dataForm.goodid" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodname">
      <el-input v-model="dataForm.goodname" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="交易价格" prop="liveprice">
      <el-input v-model="dataForm.liveprice" placeholder="交易价格"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userid">
      <el-input v-model="dataForm.userid" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="用户名称" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名称"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderid: '',
          livename: '',
          liveid: '',
          goodid: '',
          goodname: '',
          liveprice: '',
          userid: '',
          username: ''
        },
        dataRule: {
          orderid: [
            { required: true, message: '订单id不能为空', trigger: 'blur' }
          ],
          livename: [
            { required: true, message: '直播名称不能为空', trigger: 'blur' }
          ],
          liveid: [
            { required: true, message: '直播id不能为空', trigger: 'blur' }
          ],
          goodid: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          goodname: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          liveprice: [
            { required: true, message: '交易价格不能为空', trigger: 'blur' }
          ],
          userid: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '用户名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/live/liveorder/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderid = data.liveOrder.orderid
                this.dataForm.livename = data.liveOrder.livename
                this.dataForm.liveid = data.liveOrder.liveid
                this.dataForm.goodid = data.liveOrder.goodid
                this.dataForm.goodname = data.liveOrder.goodname
                this.dataForm.liveprice = data.liveOrder.liveprice
                this.dataForm.userid = data.liveOrder.userid
                this.dataForm.username = data.liveOrder.username
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/live/liveorder/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderid': this.dataForm.orderid,
                'livename': this.dataForm.livename,
                'liveid': this.dataForm.liveid,
                'goodid': this.dataForm.goodid,
                'goodname': this.dataForm.goodname,
                'liveprice': this.dataForm.liveprice,
                'userid': this.dataForm.userid,
                'username': this.dataForm.username
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
